package com.hmy.equipment.controller;


import com.hmy.equipment.domain.HmyWenshi;
import com.hmy.equipment.service.AeviceState;
import com.hmy.equipment.service.HmyWenshiService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.constant.TableName;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yyh
 * @date 2024/1/30 19:49
 * @description 环境智能监控终端（监控温度湿度）
 */
@RestController
@RequestMapping("/wenshi")
public class HmyWenshiController extends BaseController {
    @Autowired
    private HmyWenshiService wenshiService;
    @Resource
    private AeviceState aeviceState;


    /**
     * 添加环境智能监控终端
     *
     * @param wenshi 环境智能监控终端
     * @return
     */
    @Log(title = "环境智能监控终端设备添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Boolean> addGas(HmyWenshi wenshi) {
        wenshi.setCreatedBy(SecurityUtils.getUsername());
        wenshi.setUpdatedBy(SecurityUtils.getUsername());
        Boolean b = wenshiService.addWenshi(wenshi);
        aeviceState.getPhouseCoudInTable(TableName.WENSHI, wenshi.getPhouseCode());
        return R.ok(b);
    }

    /**
     * 根据环境智能监控终端列表查询
     *
     * @param wenshi 环境智能监控终端
     * @return 分页列表
     */
    @PostMapping("/list")
    public TableDataInfo selectGasList(HmyWenshi wenshi) {
        List<HmyWenshi> list = wenshiService.selectWenshiList(wenshi);
        return getDataTable(list);
    }

    /**
     * 根据id单体查询
     *
     * @param id id
     * @return 实体类数据
     */
    @PostMapping("/listById/{id}")
    public AjaxResult selectGasListAll(@PathVariable("id") Long id) {
        return AjaxResult.success(wenshiService.selectWenshiListById(id));
    }

    /**
     * 环境智能监控终端设备修改
     *
     * @param wenshi 环境智能监控终端
     * @return
     */
    @Log(title = "环境智能监控终端设备修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Boolean> editGas(HmyWenshi wenshi) {
        Boolean b = wenshiService.editWenshi(wenshi);
        aeviceState.getPhouseCoudInTable(TableName.WENSHI, wenshi.getPhouseCode());
        return R.ok(b, "修改成功");
    }

    /**
     * 环境智能监控终端设备删除
     *
     * @param ids id主键
     * @return
     */
    @Log(title = "环境智能监控终端设备删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public R<Boolean> delGas(@PathVariable(value = "ids") long[] ids) {
        String phouseCode = wenshiService.selectWenshiListById(ids[0]).getPhouseCode();
        Boolean b = wenshiService.delWenshi(ids);
        aeviceState.getPhouseCoudInTable(TableName.WENSHI, phouseCode);
        return R.ok(b, "已删除");
    }

}
